﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Log;
using MiniFox.Platform.Resources;
using MiniFox.Web.Infrastructure;

namespace MiniFox.Platform.Web
{
    /// <summary>
    /// 
    /// </summary>
    [AllowAnonymous, NoCache]
    public class ValidationController : MvcControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        [AutoWired]
        protected virtual IValidationService Component
        {
            get;
            set;
        }
        #region 绘制验证码
        /// <summary>
        /// 随机生成并绘制验证码
        /// </summary>
        /// <param name="ticket"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        [HttpGet]
        [NoLog]
        public IActionResult RandCode(string ticket, int? width = 80, int? height = 30)
        {
            var data = Component.CreateRandomCodeImage(ticket, width, height);
            return File(data, MiniFox.Resources.MimeTypes.Jpeg);
        }

        #endregion

        #region 后台验证匹配
        /// <summary>
        /// 异步模式，直接验票
        /// </summary>
        /// <param name="ticket"></param>
        /// <returns></returns>
        [HttpGet]
        public Response CheckResult(string ticket)
        {
            var response = new Response();
            if (!Component.CheckResult(ticket))
            {
                response.Code = ResErrorCode.ERR_UnmatchedValidCode;
                response.Message = ResMessage.ERR_UnmatchedValidCode;
            }
            return response;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ticket"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public Response MatchCode(string ticket, string code)
        {
            Response response = new Response();

            var success = Component.MatchCode(ticket, code, false);
            if (!success)
            {
                response.Code = ResErrorCode.ERR_UnmatchedValidCode;
                response.Message = ResMessage.ERR_UnmatchedValidCode;
            }
            return response;
        }
        #endregion

    }
}
